package org.project.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.project.api.Params.searchParams;
import org.project.api.Params.shopMesParams;
import org.project.api.Vo.*;
import org.project.mapper.CommodityMesMapper;
import org.project.pojo.ComMes;
import org.project.pojo.DataMes;
import org.project.pojo.commodityMes;
import org.project.pojo.shopMe;
import org.project.service.mainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: hzy
 * @Date: 2022/4/13 10:49
 * @Description:
 */
@Service
@Slf4j
public class mainServiceImpl implements mainService {

    @Autowired
    private CommodityMesMapper mapper;

    /**
     * @param params
     * @return
     */
    @Override
    public List<commodityMesVo> search(searchParams params) {
        List<commodityMes> mes = mapper.searchByKeyWord(params);
        List<commodityMesVo> voList = new ArrayList<>();

        for (commodityMes me : mes) {
            commodityMesVo vo = new commodityMesVo();
            vo.setComID(me.getComID());
            vo.setComName(me.getComName());
            vo.setFirst(me.getFirst());
            vo.setShopName(me.getShopName());

            int priceIsError = me.getPriceIsError();
            int salesIsError = me.getSalesIsError();

            int type = 0;
            if (priceIsError == 1 && salesIsError == 1)
                type = 3;
            else {
                if (priceIsError == 1)
                    type = 1;
                else if (salesIsError == 1)
                    type = 2;
            }
            vo.setType(type);

            voList.add(vo);
        }
        return voList;
    }

    @Override
    public DataMesVo commodityMes(String comName) {

        DataMes mes = mapper.commodityMes(comName);
//        System.out.println("mes = " + mes);
        if (mes == null)
            return null;

        DataMesVo mesVo = new DataMesVo(
                mes.getMesVo(),
                new monthQuaVo(mes.getMonthQua()),
                new monthSaleVo(mes.getMonthSale()),
                new monthComVo(mes.getMonthCom()),
                new monthInvVo(mes.getMonthInv()),
                new monthCostVo(mes.getMonthCost())
        );

        return mesVo;
    }

    @Override
    public Map<String, Object> shopMes(shopMesParams params) {

        Map<String, Object> map = new HashMap<>();

        shopMe shopMe = mapper.getShopMesByName(params.getShopName());
        map.put("shopMes", shopMe);

//        log.info("shopMe = {}", shopMe);

        List<ComMes> list;
        if (shopMe != null && shopMe.getShopID() != null) {
            list = mapper.getComMesByShopIdAndType(params, shopMe.getShopID());

            List<comMesVo> voList = new ArrayList<>();
            for (ComMes me : list) {

                comMesVo vo = new comMesVo();
                vo.setComID(me.getComID());
                vo.setComName(me.getComName());
                vo.setBrandID(me.getBrandID());

                int priceIsError = me.getPriceIsError();
                int salesIsError = me.getSalesIsError();

                int type = 0;
                if (priceIsError == 1 && salesIsError == 1)
                    type = 3;
                else {
                    if (priceIsError == 1)
                        type = 1;
                    else if (salesIsError == 1)
                        type = 2;
                }
                vo.setType(type);
                voList.add(vo);
            }

            map.put("comMes", voList);
        }

////        log.info("list = {}", list);

        return map;
    }
}
